//
//  HomePageScrollDetailViewController.m
//  InSuger
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 Dior_3g. All rights reserved.
//

#import "HomePageScrollDetailViewController.h"

@interface HomePageScrollDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSMutableArray *tableArray;

@end

@implementation HomePageScrollDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.title = self.model.title;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    
    [self createTableView];
    
    //      -----------------------------       下拉刷新    -----------------------------
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerData)];
    self.tableView.mj_header = header;
    
}

#pragma mark    下拉刷新
- (void)headerData {
    
    [self.tableView.mj_header beginRefreshing];
    [self createData];

}

#pragma mark    创建TableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(self.view);
        make.size.mas_equalTo(self.view);
        
    }];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[HomePageTableViewCell class] forCellReuseIdentifier:@"reuse"];
    self.tableView.rowHeight = self.tableView.frame.size.height / 4;
    
    [self createData];
}

#pragma mark    解析数据
- (void)createData {
    
    NSString *str = @"http://open3.bantangapp.com/topic/list?app_installtime=1448611409.218629&app_versions=4.3&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&";
    NSString *str2 = @"&os_versions=9.1&page=0&pagesize=20&screensize=750&track_device_info=iPhone7%2C2&track_deviceid=0AA403D0-5EE8-4A25-BAA9-0A37E2B9DDD3&v=7";
    [AFN getdataWithString:[NSString stringWithFormat:@"%@ids=%@%@", str, self.model.extend, str2] block:^(id block) {
        
        NSDictionary *dic = block[@"data"];
        self.tableArray = [NSMutableArray array];
        self.tableArray = [HomePageTableViewModel baseModelByArr:dic[@"topic"]];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }];

}

#pragma mark    tableView的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.tableArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HomePageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.model = self.tableArray[indexPath.row];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HomePageDetailViewController *detailVC = [[HomePageDetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
    detailVC.model = self.tableArray[indexPath.row];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
